package com.example.mystore.service.impl;

import com.example.mystore.mapper.UserMapper;
import com.example.mystore.model.User;
import com.example.mystore.service.UserService;
import com.example.mystore.config.MD5Util; // Import the MD5Util class
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public int removeUserById(int id) {
        return userMapper.markUserAsDeleted(id);  // 修改为调用逻辑删除方法
    }

    @Override
    public User register(User user) {
        user.setRole(1);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());

        // 加密
        String encryptedPassword = MD5Util.MD5Encode(user.getPassword(), "UTF-8", false);
        user.setPassword(encryptedPassword);

        userMapper.insertUser(user);
        return user;
    }

    @Override
    public User login(String account, String password) {
        User user = userMapper.getUserByUsername(account);
        if (user != null) {
            // 密码匹配
            String encryptedPassword = MD5Util.MD5Encode(password, "UTF-8", false);
            if (user.getPassword().equals(encryptedPassword)) {
                return user;
            }
        }
        return null;
    }
    public boolean validateUserInfo(String account, String phone, String email, String question, String answer) {
        User user = userMapper.getUserByUsername(account);
        if (user != null) {
            return
                    user.getPhone().equals(phone) &&
                    user.getEmail().equals(email) &&
                    user.getQuestion().equals(question) &&
                    user.getAsw().equals(answer);
        }
        return false;
    }
    @Override
    public User updateUser(User user) {
        User originalUser = userMapper.selectUserById(user.getId());
        user.setPassword(originalUser.getPassword());
        user.setCreateTime(originalUser.getCreateTime());
        user.setQuestion(originalUser.getQuestion());
        user.setAsw(originalUser.getAsw());
        user.setUpdateTime(new Date());
        int updatedRows = userMapper.updateUser(user);
        if (updatedRows > 0) {
            return userMapper.selectUserById(user.getId());
        } else {
            return null;
        }
    }

    @Override
    public User getUserById(int id) {
        return userMapper.selectUserById(id);
    }

    @Override
    public List<User> getAllUsers() {
        return userMapper.selectAllUsers();
    }

    @Override
    public boolean updatePassword(Integer id, String asw, String newPassword) {
        User user = userMapper.getUserById(id);
        if (user != null && user.getAsw().equals(asw)) {
            String encryptedNewPassword = MD5Util.MD5Encode(newPassword, "UTF-8", false);
            String updateTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            int result = userMapper.updatePassword(id, encryptedNewPassword, updateTime);
            return result > 0;
        }
        return false;
    }

    @Override
    public boolean updatePassword(String account, String newPassword) {
        // 这里可以对新密码进行加密处理
        String encryptedPassword = MD5Util.MD5Encode(newPassword, "UTF-8", false);
        int result = userMapper.updatePasswordByAccount(account, encryptedPassword);
        return result > 0;
    }
}
